﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using NIEN_LUAN.Db;
using System.Windows.Forms;

namespace NIEN_LUAN
{
    public partial class FormThongKePhieuNhap : Form
    {
        private DataContentDataContext db = new DataContentDataContext();

        public FormThongKePhieuNhap()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnXemCTPN_Click(object sender, EventArgs e)
        {
            var phieunhap = this.pHIEUNHAPBindingSource.Current as PHIEUNHAP;
            if (phieunhap != null)
            {
                Form frmSuaPN = new FormLapPhieuNhap(this, phieunhap);
                frmSuaPN.ShowDialog();
            }
            else
            {
                MessageBox.Show("Chọn phiếu nhập");
            }
            //Form frmXemCTPN = new FormChiTietPhieuNhap();
            //frmXemCTPN.ShowDialog();
        }

        private void FormThongKePhieuNhap_Load(object sender, EventArgs e)
        {
            this.dataGridViewPN.AutoGenerateColumns = false;

            var thongkephieunhap = (from tkpn in db.PHIEUNHAPs
                                    orderby tkpn.SoPhieuNhap ascending
                                    select tkpn
                                        ).ToList();
            this.pHIEUNHAPBindingSource.DataSource = thongkephieunhap;

            var nhacungcap = (from m in db.NHACUNGCAPs
                              select m).ToList();
            this.bsnhacungcap.DataSource = nhacungcap;
            this.comboBoxNCC.DataSource = bsnhacungcap;
            this.comboBoxNCC.DisplayMember = "MaNhaCungCap";

            var nhanvien = (from m in db.NHANVIENs
                            select m).ToList();
            this.bsnhanvien.DataSource = nhanvien;
            this.comboBoxNV.DataSource = bsnhanvien;
            this.comboBoxNV.DisplayMember = "MaNhanVien";
        }

        private void btnThongKe_Click(object sender, EventArgs e)
        {
            if (!this.checkBoxTimkiemMCC.Checked && this.checkBoxTkiemMNV.Checked && !this.checkBoxTkiemNgaylap.Checked)
            {
                if (string.IsNullOrEmpty(this.comboBoxNV.Text))
                {
                    MessageBox.Show("Chưa nhập mã Nhân viên");
                    return;
                }

                var phieunhap = (from m in db.PHIEUNHAPs
                                 where m.MaNhanVien == this.comboBoxNV.Text
                                 select m).ToList();
                this.pHIEUNHAPBindingSource.DataSource = phieunhap;
                this.pHIEUNHAPBindingSource.ResetBindings(true);
                return;
            }

            if (this.checkBoxTimkiemMCC.Checked && !this.checkBoxTkiemMNV.Checked && !this.checkBoxTkiemNgaylap.Checked)
            {
                if (string.IsNullOrEmpty(this.comboBoxNCC.Text))
                {
                    MessageBox.Show("Chưa nhập mã nhà cung cấp", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                var phieunhap = (from m in db.PHIEUNHAPs
                                 where m.MaNhaCungCap == this.comboBoxNCC.Text
                                 select m).ToList();
                this.pHIEUNHAPBindingSource.DataSource = phieunhap;
                this.pHIEUNHAPBindingSource.ResetBindings(true);
                return;
            }

            if (!this.checkBoxTimkiemMCC.Checked && !this.checkBoxTkiemMNV.Checked && this.checkBoxTkiemNgaylap.Checked)
            {
                var phieunhap = (from m in db.PHIEUNHAPs
                                 where ((m.NgayLap >= this.dateTimePickerNgay1.Value) && (m.NgayLap <= this.dateTimePickerNgay2.Value))
                                 select m).ToList();
                this.pHIEUNHAPBindingSource.DataSource = phieunhap;
                this.pHIEUNHAPBindingSource.ResetBindings(true);
                return;
            }

            if (!this.checkBoxTimkiemMCC.Checked && this.checkBoxTkiemMNV.Checked && this.checkBoxTkiemNgaylap.Checked)
            {
                if (string.IsNullOrEmpty(this.comboBoxNV.Text))
                {
                    MessageBox.Show("Chưa nhập mã Nhân viên");
                    return;
                }

                var phieunhap = (from m in db.PHIEUNHAPs
                                 where ((m.MaNhanVien == this.comboBoxNV.Text) && ((m.NgayLap >= this.dateTimePickerNgay1.Value) && (m.NgayLap <= this.dateTimePickerNgay2.Value)))
                                 select m).ToList();
                this.pHIEUNHAPBindingSource.DataSource = phieunhap;
                this.pHIEUNHAPBindingSource.ResetBindings(true);
                return;
            }

            if (this.checkBoxTimkiemMCC.Checked && !this.checkBoxTkiemMNV.Checked && this.checkBoxTkiemNgaylap.Checked)
            {
                if (string.IsNullOrEmpty(this.comboBoxNCC.Text))
                {
                    MessageBox.Show("Chưa nhập mã nhà cung cấp", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                var phieunhap = (from m in db.PHIEUNHAPs
                                 where ((m.MaNhaCungCap == this.comboBoxNCC.Text) && ((m.NgayLap >= this.dateTimePickerNgay1.Value) && (m.NgayLap <= this.dateTimePickerNgay2.Value)))
                                 select m).ToList();
                this.pHIEUNHAPBindingSource.DataSource = phieunhap;
                this.pHIEUNHAPBindingSource.ResetBindings(true);
                return;
            }

            if (this.checkBoxTimkiemMCC.Checked && this.checkBoxTkiemMNV.Checked && !this.checkBoxTkiemNgaylap.Checked)
            {
                if (string.IsNullOrEmpty(this.comboBoxNV.Text))
                {
                    MessageBox.Show("Chưa nhập mã Nhân viên");
                    return;
                }

                if (string.IsNullOrEmpty(this.comboBoxNCC.Text))
                {
                    MessageBox.Show("Chưa nhập mã nhà cung cấp", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                var phieunhap = (from m in db.PHIEUNHAPs
                                 where ((m.MaNhaCungCap == this.comboBoxNCC.Text) && (m.MaNhanVien == this.comboBoxNV.Text))
                                 select m).ToList();
                this.pHIEUNHAPBindingSource.DataSource = phieunhap;
                this.pHIEUNHAPBindingSource.ResetBindings(true);
                return;
            }

            if (this.checkBoxTimkiemMCC.Checked && this.checkBoxTkiemMNV.Checked && this.checkBoxTkiemNgaylap.Checked)
            {
                if (string.IsNullOrEmpty(this.comboBoxNV.Text))
                {
                    MessageBox.Show("Chưa nhập mã Nhân viên");
                    return;
                }

                if (string.IsNullOrEmpty(this.comboBoxNCC.Text))
                {
                    MessageBox.Show("Chưa nhập mã nhà cung cấp", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                var phieunhap = (from m in db.PHIEUNHAPs
                                 where ((m.MaNhaCungCap == this.comboBoxNCC.Text) && (m.MaNhanVien == this.comboBoxNV.Text) && ((m.NgayLap >= this.dateTimePickerNgay1.Value) && (m.NgayLap <= this.dateTimePickerNgay2.Value)))
                                 select m).ToList();
                this.pHIEUNHAPBindingSource.DataSource = phieunhap;
                this.pHIEUNHAPBindingSource.ResetBindings(true);
                return;
            }

            if (!this.checkBoxTimkiemMCC.Checked && !this.checkBoxTkiemMNV.Checked && !this.checkBoxTkiemNgaylap.Checked)
            {
                var chungtu = (from m in db.PHIEUNHAPs
                               select m).ToList();
                this.pHIEUNHAPBindingSource.DataSource = chungtu;
                this.pHIEUNHAPBindingSource.ResetBindings(true);
            }
        }

        private void btnChonNCC_Click(object sender, EventArgs e)
        {
            Form frmChonNCC = new FormTimKiemNhaCungCap(this);
            frmChonNCC.ShowDialog();
        }

        private void btnChonNV_Click(object sender, EventArgs e)
        {
            Form frmChonNV = new FormTimKiemNhanVien(this);
            frmChonNV.ShowDialog();
        }

        private void btnIndsPN_Click(object sender, EventArgs e)
        {
            Form frmChonPN = new FormReportPhieuNhap(this);
            frmChonPN.ShowDialog();
        }
    }
}
